package com.ihg.hiex.entity.session.before.finaltest;

import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.user.Assessor;
import com.ihg.hiex.entity.user.AssessorCertificateRecord;

/**
 * 练习
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "final_test_exercise")
public class FinalTestExercise {
    
    @Id
    @GeneratedValue
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private Session session;
    
    /** 所属assessor */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private Assessor assessor;
    
    /** 大题模板 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private FinalTestExerciseTemplate template;
    
    /** 对应的Certificate record id*/
    private Long certificateRecordId;
    
    /** 练习类型 */
    @Enumerated(EnumType.ORDINAL)
    private ExerciseType type;
    
    /** 答对题目数量 */
    private int correctCount;
    
    /** 题目总数 */
    private int scoreCount;
    
    /** 状态 */
    @Enumerated(EnumType.STRING)
    private FinalTestStatus status;
    
    /** 题号 */
    private int number;
    
    /** 重做次数 */
    private int reExerciseCount;
    
    /** 有效行为 */
    private String effective;
    
    /** 无效行为 */
    private String inEffective;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public FinalTestExercise() {
    }
    
    /**
     * 
     * @param session
     * @param assessor
     * @param template
     */
    public FinalTestExercise(Session session, Assessor assessor, FinalTestExerciseTemplate template) {
        this.session = session;
        this.assessor = assessor;
        this.template = template;
        this.type = template.getType();
        this.correctCount = 0;
        this.scoreCount = template.getTotals();
        this.number = template.getNumber();
        this.status = FinalTestStatus.NOTSTARTED;
        this.reExerciseCount = 0;
        this.disabled = false;
    }
    
    /**
     * 
     * @param assessor
     * @param template
     */
    public FinalTestExercise(Assessor assessor, FinalTestExerciseTemplate template) {
        this.assessor = assessor;
        this.template = template;
        this.type = template.getType();
        this.correctCount = 0;
        this.scoreCount = template.getTotals();
        this.number = template.getNumber();
        this.status = FinalTestStatus.NOTSTARTED;
        this.reExerciseCount = 0;
        this.disabled = false;
    }
    
    /**
     * 
     * @param assessor
     * @param template
     * @param record
     */
    public FinalTestExercise(Assessor assessor, FinalTestExerciseTemplate template, AssessorCertificateRecord record) {
        this.assessor = assessor;
        this.template = template;
        this.type = template.getType();
        this.correctCount = 0;
        this.scoreCount = template.getTotals();
        this.number = template.getNumber();
        this.certificateRecordId = record.getId();
        this.status = FinalTestStatus.NOTSTARTED;
        this.reExerciseCount = 0;
        this.disabled = false;
    }
    
    /**
     * @param correctCount
     * @return
     */
    public FinalTestExercise finish(int correctCount) {
        this.correctCount = correctCount;
        this.status = FinalTestStatus.FINISH;
        return this;
    }
    
    /**
     * 
     * @param effective
     * @param inEffective
     * @return
     */
    public FinalTestExercise editAction(String effective, String inEffective) {
        this.effective = effective;
        this.inEffective = inEffective;
        return this;
    }
    
    /**
     * get answer correct rate
     * 
     * @return
     */
    @Transient
    public BigDecimal getCorrectRate() {
        BigDecimal correctCountValue = new BigDecimal(correctCount);
        BigDecimal scoreCountValue = new BigDecimal(scoreCount);
        return correctCountValue.divide(scoreCountValue, 2, RoundingMode.HALF_UP);
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean getZeroCorrectRate() {
        BigDecimal rate = this.getCorrectRate();
        return rate.compareTo(new BigDecimal(0)) == 0;
    }
    
    /**
     * @return
     */
    @Transient
    public boolean isPass() {
        
        BigDecimal examCorrectValue = new BigDecimal(correctCount);
        BigDecimal examScoreValue = new BigDecimal(scoreCount);
        
        BigDecimal correctRate = examCorrectValue.divide(examScoreValue, 2, RoundingMode.HALF_UP);
        
        if (correctRate.compareTo(new BigDecimal("0.7")) != -1) {
            return true;
        }
        
        return false;
        
    }
    
    /**
     * 重做练习
     * 
     * @return
     */
    public FinalTestExercise reExercise() {
        this.status = FinalTestStatus.NOTSTARTED;
        this.reExerciseCount++;
        this.effective = null;
        this.inEffective = null;
        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the session
     */
    public Session getSession() {
        return session;
    }

    /**
     * @return the assessor
     */
    public Assessor getAssessor() {
        return assessor;
    }

    /**
     * @return the template
     */
    public FinalTestExerciseTemplate getTemplate() {
        return template;
    }

    /**
     * @return the certificateRecordId
     */
    public Long getCertificateRecordId() {
        return certificateRecordId;
    }

    /**
     * @return the type
     */
    public ExerciseType getType() {
        return type;
    }

    /**
     * @return the correctCount
     */
    public int getCorrectCount() {
        return correctCount;
    }

    /**
     * @return the scoreCount
     */
    public int getScoreCount() {
        return scoreCount;
    }

    /**
     * @return the status
     */
    public FinalTestStatus getStatus() {
        return status;
    }

    /**
     * @return the number
     */
    public int getNumber() {
        return number;
    }

    /**
     * @return the reExerciseCount
     */
    public int getReExerciseCount() {
        return reExerciseCount;
    }

    /**
     * @return the effective
     */
    public String getEffective() {
        return effective;
    }

    /**
     * @return the inEffective
     */
    public String getInEffective() {
        return inEffective;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param session the session to set
     */
    public void setSession(Session session) {
        this.session = session;
    }

    /**
     * @param assessor the assessor to set
     */
    public void setAssessor(Assessor assessor) {
        this.assessor = assessor;
    }

    /**
     * @param template the template to set
     */
    public void setTemplate(FinalTestExerciseTemplate template) {
        this.template = template;
    }

    /**
     * @param certificateRecordId the certificateRecordId to set
     */
    public void setCertificateRecordId(Long certificateRecordId) {
        this.certificateRecordId = certificateRecordId;
    }

    /**
     * @param type the type to set
     */
    public void setType(ExerciseType type) {
        this.type = type;
    }

    /**
     * @param correctCount the correctCount to set
     */
    public void setCorrectCount(int correctCount) {
        this.correctCount = correctCount;
    }

    /**
     * @param scoreCount the scoreCount to set
     */
    public void setScoreCount(int scoreCount) {
        this.scoreCount = scoreCount;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(FinalTestStatus status) {
        this.status = status;
    }

    /**
     * @param number the number to set
     */
    public void setNumber(int number) {
        this.number = number;
    }

    /**
     * @param reExerciseCount the reExerciseCount to set
     */
    public void setReExerciseCount(int reExerciseCount) {
        this.reExerciseCount = reExerciseCount;
    }

    /**
     * @param effective the effective to set
     */
    public void setEffective(String effective) {
        this.effective = effective;
    }

    /**
     * @param inEffective the inEffective to set
     */
    public void setInEffective(String inEffective) {
        this.inEffective = inEffective;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
    
    
}